<?php
/**
 * 前台今日课表模块
 * 展示课表功能
 */
#定义命名空间
namespace Home\Controller;
class CourseController extends ComController{
    /**
     *一周课程列表
     */
    public function index(){
        $course = M('course');//实例化课程主表
        $course_time = M('course_datetime');//实例化课程辅表
        $class_id = I('get.class_id') ? I('get.class_id') : 20;
        $prefix = C('DB_PREFIX');//读取数据库表前缀
        $time = $this->getTime();//获取本周周一与周五的时间戳
        $where = "{$prefix}course.class_id = ".$class_id;//查询条件
        $where .= " and {$prefix}course.is_del = 1";//查询条件
        $where .= " and {$prefix}course_datetime.time >= ".$time['mon'];
        $where .= " and {$prefix}course_datetime.time <= ".$time['fri'];
        $order = 'time';//排序
        $data = $course->field("{$prefix}course_datetime.*")
                       ->join("inner join {$prefix}course_datetime on {$prefix}course.id = {$prefix}course_datetime.course_id")
                       ->where($where)
                       ->order($order)
                       ->select();
        $this->assign('data',$data);
        $this->display();
    }

     /**
     * 获取本周周一与周五的时间戳
     */
    public function getTime(){
        $nowTime = time(); //时间戳
        $nowTime = date('Y-m-d',$nowTime);
        $nowTime = strtotime($nowTime);
        // $nowTime = strtotime('2017/7/1');
        $nowDay = date('N',$nowTime);//周几
        $day = 3600*24;//一天时间
        $data = array();
        switch ($nowDay) {
            case '1':
                   $data['mon'] = $nowTime;
                   $data['fri'] = $nowTime+($day*5)-1;
                break;
            case '2':
                   $data['mon'] = $nowTime-($day*1);
                   $data['fri'] = $nowTime+($day*4)-1;
                break;
            case '3':
                   $data['mon'] = $nowTime-($day*2);
                   $data['fri'] = $nowTime+($day*3)-1;
                break;
            case '4':
                   $data['mon'] = $nowTime-($day*3);
                   $data['fri'] = $nowTime+($day*2)-1;
                break;
            case '5':
                   $data['mon'] = $nowTime-($day*4);
                   $data['fri'] = $nowTime+($day*1)-1;
                break;
            case '6':
                   $data['mon'] = $nowTime-($day*5);
                   $data['fri'] = $nowTime-($day*0)-1;
                break;   
            default:
                   $data['mon'] = $nowTime-($day*6);
                   $data['fri'] = $nowTime-($day*1)-1;
                break;
        }
        return $data;
    }
}